// pages/productReleased/productReleased.js
var app = getApp();
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    title: "",
    info: "",
    point: "",
    price: "",
    type: [{
      name: "实物",
      id: 0
    }, {
      name: "虚拟",
      id: 1
    }],
    state: [{
      name: "下架",
      id: 0
    }, {
      name: "上架",
      id: 1
    }],
    productID: 0,
    category: [],
    categoryInd: -1, //类别
    typeInd: 0, //类型
    stateInd: 1, //状态
    banner: [], //轮播图片
    bannerNew: [],
    bannerAll: [],
    detail: [], //详情图片
    detailNew: [],
    detailAll: [],
    checkUp: true, //判断从编辑页面进来是否需要上传图片
    chooseViewShowDetail: true,
    chooseViewShowBanner: true,
    params: {
      productID: 0,
      contentFile: "",
      bannerFile: "",
      check: false,
    },
    dis: false,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      productID: options.productID
    })
    this.getCategory();
  },
  /**
   * 获取标题
   */
  titleBlur(e) {
    this.setData({
      title: e.detail.value
    })
  },
  /**
   * 获取商品价格
   */
  priceBlur(e) {
    this.setData({
      price: e.detail.value
    })
  },
  /**
   * 获取商品信息
   */
  infoBlur(e) {
    this.setData({
      info: e.detail.value
    })
  },
  /**
   * 获取商品卖点
   */
  pointBlur(e) {
    this.setData({
      point: e.detail.value
    })
  },
  /** 
   * 商品价格
   */
  price(e) {
    this.setData({
      price: e.detail.value
    })
  },
  /**
   * 商品类型
   */
  type(e) {
    this.setData({
      typeInd: e.detail.value
    })
  },
  /**
   * 商品状态
   */
  state(e) {
    this.setData({
      stateInd: e.detail.value
    })
  },
  /**
   * 商品类别
   */
  category(e) {
    this.setData({
      categoryInd: e.detail.value
    })
  },
  /**
   * 获取商品类别
   */
  getCategory() {
    let params = {}
    app.getCategoryList(params).then(res => {
      var good = []
      var g_type = res.data.categories[0].children //从接口中获取商品类别
      for (var i = 1; i < g_type.length; i++) { //从下标为1开始循环，不显示全部
        good[i - 1] = g_type[i] //将循环出来的数组，循环放入good中
      }
      this.setData({
        category: good
      })
      if (this.data.productID != 0) { //防止先调用详情方法而不显示类别
        if (res.state === 1) {
          this.getProductDetail();
        }
      }
    })
  },
 
  /**获取商品详情 */
  getProductDetail() {
    let params = {
      userID: app.globalData.userID,
      productID: this.data.productID
    }
    app.getReleaseProductDetail(params).then(res => {
      let product = res.data.productDetail[0]
      if (product.state) {
        this.setData({
          stateInd: 1
        })
      } else {
        this.setData({
          stateInd: 0
        })
      }
 
      let categoryInd = -1;
      for (var i = 0; i < this.data.category.length; i++) {
        if (this.data.category[i].categoryID === product.categoryID) {
          categoryInd = i
          break;
        } else {
          categoryInd: -1;
        }
      }
 
      if (product.bannerImages.length >= 2) {
        this.setData({
          chooseViewShowBanner: false
        })
      } else {
        this.setData({
          chooseViewShowBanner: true
        })
      }
 
      if (product.detailImages.length >= 3) {
        this.setData({
          chooseViewShowDetail: false
        })
      } else {
        this.setData({
          chooseViewShowDetail: true
        })
      }
      this.setData({
        title: product.title,
        info: product.info,
        point: product.point,
        typeInd: product.productType,
        price: product.currentPrice,
        banner: product.bannerImages,
        detail: product.detailImages,
        categoryInd: categoryInd
      })
    })
  },
 
  /**发布提交 */
  formSubmit(e) {
    let that = this
    var priceTF = /^\d+(\.\d{1,2})?$/
    if (e.detail.value.title === "") {
      wx.showToast({
        title: '请输入商品名称',
        icon: "none",
        duration: 1000,
        mask: true,
      })
    } else if (e.detail.value.title.length > 60) {
      wx.showToast({
        title: '商品名称不得大于60字',
        icon: "none",
        duration: 1000,
        mask: true,
      })
    } else if (e.detail.value.title.length === "") {
      wx.showToast({
        title: '请输入商品价格',
        icon: "none",
        duration: 1000,
        mask: true,
      })
    } else if (!priceTF.test(e.detail.value.price)) {
      wx.showToast({
        title: '商品价格精确到两位',
        icon: "none",
        duration: 1000,
        mask: true,
      })
    } else if (e.detail.value.info === "") {
      wx.showToast({
        title: '请输入商品信息',
        icon: "none",
        duration: 1000,
        mask: true,
      })
    } else if (e.detail.value.point === "") {
      wx.showToast({
        title: '请输入商品卖点',
        icon: "none",
        duration: 1000,
        mask: true,
      })
    } else if (that.data.categoryInd === -1) {
      wx.showToast({
        title: '请选择商品类别',
        icon: "none",
        duration: 1000,
        mask: true,
      })
    } else if (that.data.typeInd === -1) {
      wx.showToast({
        title: '请选择商品类型',
        icon: "none",
        duration: 1000,
        mask: true,
      })
    } else if (that.data.stateInd === -1) {
      wx.showToast({
        title: '请选择商品状态',
        icon: "none",
        duration: 1000,
        mask: true,
      })
    } else if (that.data.banner.length === 0) {
      wx.showToast({
        title: '请选择轮播图片',
        icon: "none",
        duration: 1000,
        mask: true,
      })
    } else if (that.data.detail.length === 0) {
      wx.showToast({
        title: '请选择详情图片',
        icon: "none",
        duration: 1000,
        mask: true,
      })
    } else {
      let params = {
        userID: app.globalData.userID,
        productID: that.data.productID,
        title: e.detail.value.title,
        price: e.detail.value.price,
        info: e.detail.value.info,
        point: e.detail.value.point,
        categoryID: that.data.category[that.data.categoryInd].categoryID,
        productType: that.data.type[that.data.typeInd].id,
        state: that.data.state[that.data.stateInd].id
      }
      wx.showModal({
        title: '提示',
        content: '确定发布商品',
        success(res) {
          if (res.confirm) {
            if (that.data.productID != 0) {
              that.sureEdit(params); //编辑
            } else {
              that.sureRelease(params); //发布
            }
            that.setData({
              dis: true,
            })
          }
        }
      })
    }
  },
 
  /**确认发布 */
  sureRelease(params) {
    let that = this
    app.addProduct(params).then(res => {
      that.data.params.productID = res.data.productID;
      that.data.params.bannerFile = res.data.bannerFile;
      that.data.params.contentFile = res.data.contentFile;
      for (var i = 0; i < that.data.banner.length; i++) {
        wx.uploadFile({
          url: app.globalData.baseUrl + '/wechat/release/addProductPhoto',
          filePath: that.data.banner[i],
          name: 'banner',
          formData: {
            'parameters': JSON.stringify(that.data.params)
          },
        })
        if (that.data.banner.length === i + 1) {
          for (var j = 0; j < that.data.detail.length; j++) {
            if (that.data.detail.length === j + 1) {
              that.data.params.check = true
            }
            wx.uploadFile({
              url: app.globalData.baseUrl + '/wechat/release/addProductPhoto',
              filePath: that.data.detail[j],
              name: 'detail',
              formData: {
                'parameters': JSON.stringify(that.data.params)
              },
              success: function(res) {
                if (JSON.parse(res.data).state === 1) {
                  wx.showToast({
                    title: '商品发布成功',
                    icon: "none",
                    duration: 2000,
                    mask: true,
                    success() {
                      setTimeout(function() {
                        wx.navigateBack({
                          delta: 0,
                        })
                      }, 1000);
                    }
                  })
                } else {
                  wx.showToast({
                    title: '商品发布失败，请稍后再试',
                    icon: "none",
                    duration: 2000,
                    mask: true,
                    success() {
                      setTimeout(function() {
                        wx.navigateBack({
                          delta: 0,
                        })
                      }, 1000);
                    }
                  })
                }
              },
              fail: function(res) {
                if (JSON.parse(res.errMsg) === "request:fail socket time out timeout:6000") {
                  wx.showToast({
                    title: '请求超时，请稍后再试！',
                    icon: "none",
                    duration: 2000,
                    mask: true,
                    success() {
                      setTimeout(function() {
                        wx.navigateBack({
                          delta: 0,
                        })
                      }, 1000);
                    }
                  })
                }
              }
            })
          }
        }
      }
    })
  },
 

})